package com.cheer.controller;

import com.cheer.common.Page;
import com.cheer.common.Result;
import com.cheer.dto.PasswordUpdateDTO;
import com.cheer.dto.OaAdminAddDTO;
import com.cheer.dto.OaAdminUpdateDTO;
import com.cheer.security.UserInfoHolder;
import com.cheer.service.OaAdminRoleService;
import com.cheer.service.OaAdminService;
import com.cheer.vo.OaAdminInfoVO;
import com.cheer.vo.OaAdminListVO;
import com.cheer.vo.SimpleObjectVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/admin")
public class OaAdminController {

    @Autowired
    private UserInfoHolder userInfoHolder;

    @Autowired
    private OaAdminService oaAdminService;

    @Autowired
    private OaAdminRoleService oaAdminRoleService;

    @ApiOperation("添加管理员")
    @PostMapping("/add")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result add(@RequestBody @Valid OaAdminAddDTO adminAddDTO) {
        oaAdminService.add(adminAddDTO);
        return Result.success();
    }

    @ApiOperation("修改密码")
    @PostMapping("/password/update")
    public Result updatePassword(@RequestBody @Valid PasswordUpdateDTO passwordUpdateDTO){
        oaAdminService.updatePassword(passwordUpdateDTO);
        return Result.success();
    }

    @ApiOperation("查看名称")
    @GetMapping("/name")
    public Result<String> name(){
        return Result.success(userInfoHolder.getAdmin().getAdName());
    }

    @ApiOperation("查询权限列表")
    @GetMapping("/role")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<List<SimpleObjectVO>> role() {
        return Result.success(oaAdminRoleService.listSimple());
    }

    @ApiOperation("查询管理员列表")
    @GetMapping("/list")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<Page<OaAdminListVO>> list(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                            @RequestParam String keyword) {
        return Result.success(oaAdminService.list(pageNum, pageSize, keyword));
    }

    @ApiOperation("批量删除")
    @PostMapping("/delete")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result delete(@RequestBody List<Long> ids) {
        oaAdminService.delete(ids);
        return Result.success();
    }

    @ApiOperation("查询管理员详情")
    @GetMapping("/info")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<OaAdminInfoVO> info(@RequestParam Integer id) {
        return Result.success(oaAdminService.info(id));
    }

    @ApiOperation("修改管理员")
    @PostMapping("/update")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result update(@RequestBody @Valid OaAdminUpdateDTO adminUpdateDTO) {
        oaAdminService.update(adminUpdateDTO);
        return Result.success();
    }

    @ApiOperation("密码重置")
    @GetMapping("/reset")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result reset(@RequestParam Integer id) {
        oaAdminService.reset(id);
        return Result.success();
    }

    @ApiOperation("启用或禁用")
    @GetMapping("/status")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result updateStatus(@RequestParam Integer id, @RequestParam Integer status) {
        oaAdminService.updateStatus(id, status);
        return Result.success();
    }

    @ApiOperation("查询管理员所属单位")
    @GetMapping("/pid")
    public Result<Integer> pid() {
        return Result.success(userInfoHolder.getAdmin().getPid());
    }
}
